<template>
      <div class="result-container">
    <div class="title-wrap">
      <!-- 标题 -->
      <h2 class="title">{{ $route.query.q }}</h2>
      <span class="sub-title">找到 {{ count }} 个结果</span>
    </div>
    <el-tabs v-model="activeIndex">
      <el-tab-pane label="歌曲" name="songs">
        <table class="el-table">
          <thead>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr
              v-for="(item, index) in songList"
              :key="index"
              class="el-table__row"
              @dblclick="playMusic(item.id)"
            >
              <td>{{index+1}}</td>
              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <!-- 歌名 -->
                    <span>{{ item.name }}</span>
                    <!-- mv的图标 -->
                    <span v-if="item.mvid != 0" class="iconfont icon-mv"></span>
                  </div>
                  <!-- 二级标题 -->
                  <span v-if="item.alias.length != 0">{{ item.alias[0] }}</span>
                </div>
              </td>
              <td>{{ item.artists[0].name }}</td>
              <td>{{ item.album.name }}</td>
              <td>{{ item.duration }}</td>
            </tr>
          </tbody>
        </table>
      </el-tab-pane>
      <el-tab-pane label="歌单" name="lists">
        <div class="items">
          <div
            v-for="(item, index) in playlists"
            :key="index"
            class="item"
            @click="toPlaylist(item.id)"
          >
            <div class="img-wrap">
              <div class="num-wrap">
                播放量:
                <span class="num">{{ item.playCount }}</span>
              </div>
              <img :src="item.coverImgUrl" alt="" />
              <span class="iconfont icon-play"></span>
            </div>
            <p class="name">{{ item.name }}</p>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="MV" name="mv">
        <div class="items mv">
          <div v-for="(item, index) in mv" :key="index" class="item" @click="toMV(item.id)">
            <div class="img-wrap">
              <!-- 封面 -->
              <img :src="item.cover" alt="" />
              <span class="iconfont icon-play"></span>
              <div class="num-wrap">
                <div class="iconfont icon-play"></div>
                <!-- 播放次数 -->
                <div class="num">{{ item.playCount }}</div>
              </div>
              <!-- 持续时间 -->
              <span class="time">{{ item.duration }}</span>
            </div>
            <div class="info-wrap">
              <!-- mv名 -->
              <div class="name">{{ item.name }}</div>
              <!-- 歌手名 -->
              <div class="singer">{{ item.artistName }}</div>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import {getsearchMultidata} from '../../axios/search'
import {getplayMultidata} from '../../axios/play'
export default {
    name: 'VueReslut',

    data() {
        return {
           activeName:'second',
           activeIndex:'songs',
           type:1,
           q:'',
           songList:[],
           playlists:[],
           mv:[],
           count:0
        };
    },

    created () {   
        // console.log(this.$route.query.q);
        getsearchMultidata(this.$route.query.q,this.type).then(res=>{
            // console.log(res);
            this.count = res.result.songCount
            this.songList = res.result.songs
             for (let i = 0; i < this.songList.length; i++) {
          let min = parseInt(this.songList[i].duration / 1000 / 60)
          let sec = parseInt((this.songList[i].duration / 1000) % 60)
          if (min < 10) {
            min = '0' + min
          }
          if (sec < 10) {
            sec = '0' + sec
          }
          // console.log(min + '|' + sec)
          this.songList[i].duration = min + ':' + sec
        }
        })
    },

    updated() {
    
    },

    watch: {
      activeIndex(){
           switch (this.activeIndex) {
            case 'songs':
                this.type = 1  
                 break; 
            case 'lists':
                this.type = 1000
                 break;
            case 'mv':
                this.type = 1004
                 break;
            default:
                break;
        }
        // console.log(this.type);
        if (this.type === 1000){
            getsearchMultidata(this.$route.query.q,this.type).then(res=>{
            // console.log(res);
            this.count = res.result.playlistCount
            this.playlists = res.result.playlists
             for (let i = 0; i < this.playlists.length; i++) {
              if (this.playlists[i].playCount > 100000) {
                this.playlists[i].playCount =
                  parseInt(this.playlists[i].playCount / 10000) + '万'
              }
            }
        })
        }else if (this.type === 1004){
            getsearchMultidata(this.$route.query.q,this.type).then(res=>{
            // console.log(res);
            this.count = res.result.mvCount
            this.mv = res.result.mvs
               for (let i = 0; i < this.mv.length; i++) {
              // 时间
              let min = parseInt(this.mv[i].duration / 1000 / 60)
              let sec = parseInt((this.mv[i].duration / 1000) % 60)
              if (min < 10) {
                min = '0' + min
              }
              if (sec < 10) {
                sec = '0' + sec
              }
              this.mv[i].duration = min + ':' + sec

              // 播放次数
              if (this.mv[i].playCount > 100000) {
                this.mv[i].playCount =
                  parseInt(this.mv[i].playCount / 10000) + '万'
              }
            }
        })
        }else if (this.type === 1){
             getsearchMultidata(this.$route.query.q,this.type).then(res=>{
            // console.log(res);
            this.count = res.result.songCount
            this.songList = res.result.songs
             for (let i = 0; i < this.songList.length; i++) {
          let min = parseInt(this.songList[i].duration / 1000 / 60)
          let sec = parseInt((this.songList[i].duration / 1000) % 60)
          if (min < 10) {
            min = '0' + min
          }
          if (sec < 10) {
            sec = '0' + sec
          }
          // console.log(min + '|' + sec)
          this.songList[i].duration = min + ':' + sec
        }
        })
        }
      }  
    },

    methods: {
        playMusic(id){
             getplayMultidata(id).then(res=>{
            //   this.url = res.data[0].url
            // console.log(res);
            this.$parent.musicUrl = res.data[0].url
            // console.log(this.$parent);
          })
        },
        toPlaylist(id){
            this.$router.push(`/playlists?q=${id}`)
            // console.log(this.$route.query.q);
        },
        toMV(id){
            this.$router.push(`/mvss?q=${id}`)
        }
    },
};
</script>

<style lang="scss" scoped>


.el-tabs__active-bar {
  background-color: #c3473a;
}

.el-tabs__item:hover,
.el-tabs__item.is-active {
  color: #c3473a;
}

.el-table th > .cell {
  font-weight: normal;
  color: black;
}

.el-pagination {
  margin-top: 50px;
  text-align: center;
}

.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #dd6d60;
}

.el-table {
  border-bottom: none;
  border-collapse: collapse;
}

.el-table.playlit-table th:nth-child(2) {
  width: 130px;
}

.el-table.playlit-table th:nth-child(3) {
  width: 300px;
}

.el-table.playlit-table th:nth-child(4) {
  width: 200px;
}

.el-table tbody {
  border-bottom: none;
}

.el-table td {
  border-bottom: none;
}

.el-table td:first-child {
  padding-left: 10px;
}

.el-table th {
  font-weight: normal;
}

.el-table th:first-child {
  width: 50px;
}

.el-table th:nth-child(2) {
  width: 300px;
}

.el-table th:nth-child(3) {
  width: 200px;
}

.el-table .song-wrap > span {
  margin-top: 20px;
  display: inline-block;
  color: #bebebe;
}

.el-table .song-wrap .icon-mv {
  padding-left: 5px;
  color: #dd6d60;
}

.el-table .img-wrap {
  position: relative;
  width: 70px;
  height: 70px;
}

.el-table .img-wrap img {
  width: 70px;
  height: 70px;
  border-radius: 5px;
}

.el-table .img-wrap .icon-play {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 25px;
  height: 25px;
  color: #dd6d60;
  font-size: 12px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.8);
}

.el-table .img-wrap .icon-play::before {
  transform: translateX(1px);
}

.el-table tr:nth-child(2n) {
  background-color: #fafafa;
}

.el-table tr:hover {
  background-color: #f5f7fa;
}

.result-container .title-wrap {
  display: flex;
  align-items: flex-end;
  margin-bottom: 30px;
}

.result-container .title-wrap .title {
  margin-right: 10px;
}

.result-container .title-wrap .sub-title {
  font-size: 15px;
  color: #bebebe;
}

.result-container .el-tabs__item {
  font-size: 18px;
}

.result-container .el-table td,
.result-container .el-table th.is-leaf {
  border-bottom: none;
}

.result-container .items {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
}

.result-container .items .item {
  width: 200px;
  cursor: pointer;
  margin-right: 20px;
  margin-bottom: 20px;
}

.result-container .items .item .img-wrap {
  width: 100%;
  position: relative;
}

.result-container .items .item .img-wrap > .icon-play {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 40px;
  height: 40px;
  color: #dd6d60;
  font-size: 20px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.8);
  opacity: 0;
}


.result-container .items .item .img-wrap:hover > .icon-play {
  opacity: 1;
}

.result-container .items .item .img-wrap img {
  width: 100%;
  border-radius: 5px;
}

.result-container .items .item .img-wrap .num-wrap {
  position: absolute;
  color: white;
  top: 0;
  right: 0;
  display: flex;
  align-items: center;
  font-size: 15px;
  padding-right: 5px;
  padding-top: 2px;
}

.result-container .items .item .img-wrap .num-wrap .icon-play {
  font-size: 12px;
  display: flex;
  align-items: center;
  margin-right: 5px;
}

.result-container .items .item .img-wrap .time {
  position: absolute;
  bottom: 5px;
  right: 5px;
  color: white;
  font-size: 15px;
}

.result-container .items .item .name {
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.result-container .items .item .singer {
  font-size: 14px;
  color: #c5c5c5;
}

.result-container .items.mv .item {
  width: 250px;
}
</style>